import qiniu

access_key = "W0oGRaBkAhrcppAbz6Nc8-q5EcXfL5vLRashY4SI"
secret_key = "tsYCBckepW4CqW0uHb9RdfDMXRDOTEpYecJAMItL"


def pic_storge(data):
    """存储图片数据到七牛云"""
    # 验证操作，让qiniu知道我们是注册过的用户
    try:
        q = qiniu.Auth(access_key, secret_key)
        bucket_name = "python-ihome"
        token = q.upload_token(bucket_name)
        ret, info = qiniu.put_data(token, None, data)
    except Exception as e:
        raise e

    """
    req_id:tD0AAAeBZ87hID0V, text_body:{"hash":"FtVYN3xbCAakWMuG_DQIjm7pm0NM",
    "key":"FtVYN3xbCAakWMuG_DQIjm7pm0NM"}, _ResponseInfo__response:<Response [200]>, x_log:body;0s.ph;0s.put.in;0s.put.disk:3;1s.put.in;1s.put.disk:7;1s.ph;PFDS:8;0s.put.out:3;PFDS:12;body;rs38_6.sel:4/not found;rs37_6.sel/not found;rdb.g/no such key;DBD/404;v4.get/Document not found;rs38_6.ins:15;rwro.ins:20;RS:20;rs.put:21;rs-upload.putFile:34;UP:54, exception:None,
     status_code:200

    """
    if ret is not None:
        if info.status_code == 200:
            print(ret)
            return ret["key"]
    else:
        raise Exception("七牛云第三方错误")


if __name__ == '__main__':
    file = input("请输入图片的地址:")
    with open(file,"rb") as f:
        pic_storge(f.read())